﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Daco.Web.UI;
using Daco.Web.Store;

namespace Daco.Web.Controls
{
    public partial class RelatedProduct : BaseUserControl
    {
        private bool _showBox = true;
        public bool ShowBox
        {
            get { return _showBox; }
            set { _showBox = value; }
        }

        private CProduct _product = null;
        public CProduct Product
        {
            get { return _product; }
            set { _product = value; }
        }

        private int _itemCount = 8;
        public int ItemCount
        {
            get { return _itemCount; }
            set { _itemCount = value; }
        }

        public RelatedProduct()
        {
            this.Load += new EventHandler(RelatedProduct_Load);
        }

        public RelatedProduct(CProduct product)
            : this()
        {
            this.Product = product;
        }

        public RelatedProduct(CProduct product, int itemCount)
            : this(product)
        {
            this.ItemCount = itemCount;
        }

        void RelatedProduct_Load(object sender, EventArgs e)
        {
            if (Product != null)
            {
                rpProduct.DataSource = CProduct.GetRelatedProducts(Product.ProductID, Product.ProductCategory.CategoryID, Product.Manufacture.ManufactureID, ItemCount);
                rpProduct.DataBind();
            }
        }

        protected string GetPhotoUrl(object item)
        {
            Daco.Web.Store.CProduct p = (Daco.Web.Store.CProduct)item;
            return Page.ResolveUrl(string.Format("~/Handlers/PHOTOHandler.ashx?pid={0}", p.GetDefaulPhoto().PhotoID));
        }

        protected string GetPriceDetail(object item)
        {
            CProduct current = (CProduct)item;

            System.Text.StringBuilder sb = new System.Text.StringBuilder();

            if (!current.EnablePriceConfidential && current.UnitPrice > 0)
            {
                sb.Append(@"<b>Giá: </b><label style=""color: Red;"">");
                sb.Append(current.UnitPrice.ToString(Config.CSiteConfig.Current.DoubleFormatString));
                sb.Append(current.Currency);
                sb.Append("</label>");
            }

            return sb.ToString();
        }
    }
}